#include "HelloWorldScene.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "MoveUp.h"
#include "Collision.h"
USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
	
    /////////////////////////////
    // 3. add your codes below...

	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("NewAnimation0.plist");
	isAddMonkey = false;
	fSetSizeScreen();
	mGameOver = 0;
	fAddButton();
	fAddMonkey();
    fAddTree();
	fAddBranch();
	fAddCollision();
	fAddScore();
	
    return true;
}


void HelloWorld::onEnter()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCLayer::onEnter();
}

/****************************************************
* Name			: Set SizeScreen
* Param			: 
* Mission		: set value for variable mSizeScreen
* Modify date	: 15/5/2014
* Author		: Phuoctd
****************************************************/
void HelloWorld::fSetSizeScreen()
{
	mSizeScreen = CCDirector::sharedDirector()->getWinSize();
}

/****************************************************
* Name			: Add Monkey
* Param			: 
* Mission		: Add the monkey and set attribute for monkey
* Modify date	: 15/5/2014
* Author		: Phuoctd
****************************************************/
void HelloWorld::fAddMonkey()
{
	mMonkey = new Monkey();
	cocos2d::extension::CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("NewAnimation0.png","NewAnimation0.plist","NewAnimation.ExportJson");
	mMonkey = (Monkey * ) cocos2d::extension::CCArmature::create("NewAnimation");
	mMonkey->getAnimation()->playByIndex(0);
	mMonkey->setScale(0.3f);
	mMonkey->setPosition(mSizeScreen.width*3/4 , mSizeScreen.height/2);
	mMonkey->fInitAttribute();
	if(!isAddMonkey)
	{
		this->addChild(mMonkey,2);
		isAddMonkey = true;
	}
	
}

/****************************************************
* Name			: Add Tree
* Param			: 
* Mission		: Add the tree to screen
* Modify date	: 15/5/2014
* Author		: Phuoctd
****************************************************/
void HelloWorld::fAddTree()
{
	tree = CCSprite::createWithSpriteFrameName("tree.png");
	tree->CCNode::setPosition(mSizeScreen.width/2 , mSizeScreen.height/2);
	this->addChild(tree);

}

/****************************************************
* Name			: Add Branch
* Param			: 
* Mission		: add all branch to map
* Modify date	: 15/5/2014
* Author		: Phuoctd
****************************************************/
void HelloWorld::fAddBranch()
{
	int arrRandom[5];
	for(int i = 0 ; i < 3 ; i++)
	{
		arrRandom[i] = rand() % 10;
		if(arrRandom[i]%2 == 0)
		{
			arrRandom[i] = mSizeScreen.width/2 -kPosition;
		}
		else
		{
			arrRandom[i] = mSizeScreen.width/2 + kPosition;
		}
	}


	mBranch1 = new Branch();
	mBranch1->CCNode::setPosition(arrRandom[0] ,100);
	mBranch1->addComponent(new MoveUp());
	this->addChild(mBranch1,3);

	mBranch2 = new Branch();
	mBranch2->CCNode::setPosition(arrRandom[1],-100);
	mBranch2->addComponent(new MoveUp());
	this->addChild(mBranch2 , 3);

	mBranch3 = new Branch();
	mBranch3->CCNode::setPosition(arrRandom[2],-300);
	mBranch3->addComponent(new MoveUp());
	this->addChild(mBranch3 , 3);
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author			: Phuoctd
****************************************************/
void HelloWorld::fAddButton()
{
	CCMenuItemImage *cmdTurnLeft = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::fCmdTurnLeftOnClick));
    
	cmdTurnLeft->setPosition(ccp( 30 , 50));

	CCMenuItemImage *cmdTurnRight = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::fCmdTurnRightOnClick));
    
	cmdTurnRight->setPosition(ccp( mSizeScreen.width/2 + 100 , 50));

	CCMenu* pMenuLeft = CCMenu::create(cmdTurnLeft, NULL);
    pMenuLeft->setPosition(CCPointZero);
    this->addChild(pMenuLeft, 1);

	CCMenu* pMenuRight = CCMenu::create(cmdTurnRight, NULL);
    pMenuRight->setPosition(CCPointZero);
    this->addChild(pMenuRight, 1);

}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fCmdTurnLeftOnClick(CCObject* pSender)
{
	if(mGameOver==1)
	{
		return;
	}
	mMonkey->setPositionX(mSizeScreen.width/5 );
	mMonkey->fTurnLeft();
	
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fCmdTurnRightOnClick(CCObject* pSender)
{
	if(mGameOver==1)
	{
		return;
	}
	mMonkey->setPositionX(mSizeScreen.width*3/4 );
	mMonkey->fTurnRight();
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fAddCollision()
{
	mBranch1->addComponent(new Collision(mMonkey));
	mBranch2->addComponent(new Collision(mMonkey));
	mBranch3->addComponent(new Collision(mMonkey));
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fGameOver()
{
	mGameOver = 1;
	((MoveUp *)mBranch1->getComponent("MoveUp"))->fStopAction();
	((MoveUp *)mBranch2->getComponent("MoveUp"))->fStopAction();
	((MoveUp *)mBranch3->getComponent("MoveUp"))->fStopAction();
	if(mMonkey->fGet_mPosition() == 1)
	{
		mMonkey->getAnimation()->playByIndex(2);
	}
	else
	{
		mMonkey->getAnimation()->playByIndex(3);
	}

	mMonkey->runAction(CCMoveTo::create(0.5, ccp( mMonkey->getPositionX() , -20)));

}


/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fResetGame()
{	
	mGameOver = 0;
	mScore = 0;

	int arrRandom[5];
	for(int i = 0 ; i < 3 ; i++)
	{
		arrRandom[i] = rand() % 10;
		if(arrRandom[i]%2 == 0)
		{
			arrRandom[i] = mSizeScreen.width/2 -kPosition;
		}
		else
		{
			arrRandom[i] = mSizeScreen.width/2 + kPosition;
		}
	}
	mBranch1->CCNode::setPosition(arrRandom[0] ,100);
	mBranch2->CCNode::setPosition(arrRandom[1],-100);
	mBranch3->CCNode::setPosition(arrRandom[2],-300);
	mBranch1->mStatus = isIdle;
	mBranch2->mStatus = isIdle;
	mBranch3->mStatus = isIdle;
	mBranch1->mLevel = 1;
	mBranch2->mLevel = 1;
	mBranch3->mLevel = 1;
	mMonkey->fInitAttribute();
	mMonkey->getAnimation()->playByIndex(0);
	mMonkey->setPosition(mSizeScreen.width*3/4 , mSizeScreen.height/2);
	fUpdateScore();
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
	if(mGameOver == 1)
	{
		//fRemoveAllChild();
		fResetGame();
		return true;
	}
	return false;
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fAddScore()
{
	CCString* strp = CCString::createWithFormat("%d", mMonkey->fGetScore());
    mlblScore = CCLabelTTF::create(strp->getCString(), "", 30);
    mlblScore->setZOrder(getChildrenCount()); //font of all
	mlblScore->CCNode::setPosition(mSizeScreen.width/2, mSizeScreen.height - 20);
    
    this->addChild(mlblScore);
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fUpdateScore()
{
	CCString* strp = CCString::createWithFormat("%d", mMonkey->fGetScore());
    mlblScore->setString(strp->getCString());
	if(mMonkey->fGetScore() % 10 == 0)
	{
		mBranch1->mLevel =1 + mMonkey->fGetScore()/ 10;
		mBranch2->mLevel =1 + mMonkey->fGetScore()/ 10;
		mBranch3->mLevel =1 + mMonkey->fGetScore()/ 10;
	}
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void HelloWorld::fRemoveAllChild()
{
	this->removeChild(mlblScore);
	this->removeChild(mBranch1);
	this->removeChild(mBranch2);
	this->removeChild(mBranch3);
	this->removeChild(tree);
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}


